<?php

class Adept_Template_Dictionary 
{
    protected $tags = array();
    protected $modificators = array();
    protected $aliases = array();
    
    static protected $instance = null;
    
    static public function getInstance() 
    {
    	if (self::$instance == null) {
    		self::$instance = new self();
    	}
    	return self::$instance;
    }
    
    /**
     * @param string $tag
     * @return Adept_Template_TagInfo
     */
    public function getTagInfo($tag) 
    {
        $tag = strtolower($tag);
    	return isset($this->tags[$tag]) ? $this->tags[$tag] : null;
    }	
    
    public function setTagInfo($tag, $info) 
    {
    	$this->tags[strtolower($tag)] = $info;
    }	
    
    public function removeTag($tag) 
    {
    	unset($this->tags[strtolower($tag)]);
    }	
    
    
    /**
     * @param string $tag
     * @return Adept_Template_ModificatorInfo
     */
    public function getModificatorInfo($alias) 
    {
        $alias = strtolower($alias);
    	return isset($this->modificators[$alias]) ? $this->modificators[$alias] : null;
    }	
    
    public function setModificatorInfo($alias, $info) 
    {
    	$this->modificators[strtolower($alias)] = $info;
    }	    
    
    public function importLibrary($lib) 
    {
        $file = $lib . '.config.php';
        
        $config = null;
        //Adept::loadFile($file, array(), false, false);
        include($file);
        
        if ($config == null) {
            throw new Adept_Exception("Invalid config file: " . $file);
        }
        
        $tagsInfo = isset($config['tags']) ? $config['tags'] : array();
        $modificatorsInfo = isset($config['modificators']) ? $config['modificators'] : array();
        
        foreach ($tagsInfo as $alias => $options) {
            $alias = strtolower($alias);
            $info = new Adept_Template_TagInfo();
            $info->setAlias($alias);
            if (!isset($options['class'])) {
                throw new Adept_Exception('Class property not defined for tag ' . $alias);
            }
            $info->setClass($options['class']);
            if (isset($options['attributes'])) {
                $info->setAttributes($options['attributes']);
            }
            if (isset($options['parameters'])) {
                $info->setParameters($options['parameters']);
            }
            if (isset($options['hasClosingTag'])) {
                $info->setHasClosingTag($options['hasClosingTag']);
            }
            $this->tags[$alias] = $info;
        }
        
        foreach ($modificatorsInfo as $alias => $options) {
            $info = new Adept_Template_ModificatorInfo();
            $info->setAlias($alias);   
            if (!isset($options['class'])) {
                throw new Adept_Exception('Class property not defined for modificator ' . $alias);
            }
            $info->setClass($options['class']);
            if (isset($options['parameters'])) {
                $info->setParameters($options['parameters']);
            }
            if (isset($options['minParams'])) {
                $info->setMinParams($options['minParams']);
            }
            if (isset($options['maxParams'])) {
                $info->setMaxParams($options['maxParams']);
            }
            $this->modificators[$alias] = $info;
        }
        
    }
}